﻿using QLicense;
using System;

namespace MultiDAQ_Analysis.License
{
    /// <summary>
    /// 扩展的许可证状态
    /// 补充QLicense原生LicenseStatus的功能
    /// </summary>
    public enum ExtendedLicenseStatus
    {
        /// <summary>
        /// 未定义状态
        /// </summary>
        Undefined = 0,

        /// <summary>
        /// 许可证有效
        /// </summary>
        Valid = 1,

        /// <summary>
        /// 许可证无效
        /// </summary>
        Invalid = 2,

        /// <summary>
        /// 许可证被破解或篡改
        /// </summary>
        Cracked = 3,

        /// <summary>
        /// 许可证已过期
        /// </summary>
        Expired = 4,

        /// <summary>
        /// 许可证即将过期
        /// </summary>
        ExpiringSoon = 5,

        /// <summary>
        /// 设备不匹配
        /// </summary>
        DeviceMismatch = 6,

        /// <summary>
        /// 许可证文件不存在
        /// </summary>
        FileNotFound = 7,

        /// <summary>
        /// 许可证管理器未初始化
        /// </summary>
        NotInitialized = 8
    }

    /// <summary>
    /// 许可证状态信息类
    /// </summary>
    public class LicenseStatusInfo
    {
        /// <summary>
        /// 原始的QLicense状态
        /// </summary>
        public LicenseStatus OriginalStatus { get; set; }

        /// <summary>
        /// 扩展的状态
        /// </summary>
        public ExtendedLicenseStatus ExtendedStatus { get; set; }

        /// <summary>
        /// 状态消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 检查时间
        /// </summary>
        public DateTime CheckTime { get; set; }

        /// <summary>
        /// 许可证信息
        /// </summary>
        public MyTimeLicense LicenseInfo { get; set; }

        /// <summary>
        /// 是否为有效许可证
        /// </summary>
        public bool IsValid => ExtendedStatus == ExtendedLicenseStatus.Valid ||
                               ExtendedStatus == ExtendedLicenseStatus.ExpiringSoon;

        /// <summary>
        /// 是否需要用户关注
        /// </summary>
        public bool NeedsAttention => ExtendedStatus == ExtendedLicenseStatus.ExpiringSoon ||
                                     ExtendedStatus == ExtendedLicenseStatus.Expired ||
                                     ExtendedStatus == ExtendedLicenseStatus.Invalid;

        public LicenseStatusInfo()
        {
            CheckTime = DateTime.Now;
        }

        /// <summary>
        /// 获取状态的友好描述
        /// </summary>
        /// <returns></returns>
        public string GetStatusDescription()
        {
            switch (ExtendedStatus)
            {
                case ExtendedLicenseStatus.Valid:
                    return "许可证有效";
                case ExtendedLicenseStatus.ExpiringSoon:
                    return "许可证即将过期";
                case ExtendedLicenseStatus.Expired:
                    return "许可证已过期";
                case ExtendedLicenseStatus.Invalid:
                    return "许可证无效";
                case ExtendedLicenseStatus.Cracked:
                    return "许可证被破解或篡改";
                case ExtendedLicenseStatus.DeviceMismatch:
                    return "设备不匹配";
                case ExtendedLicenseStatus.FileNotFound:
                    return "许可证文件不存在";
                case ExtendedLicenseStatus.NotInitialized:
                    return "许可证管理器未初始化";
                case ExtendedLicenseStatus.Undefined:
                    return "状态未定义";
                default:
                    return "未知状态";
            }
        }

        /// <summary>
        /// 获取状态图标类型
        /// </summary>
        /// <returns>图标类型：success, warning, error</returns>
        public string GetIconType()
        {
            switch (ExtendedStatus)
            {
                case ExtendedLicenseStatus.Valid:
                    return "success";
                case ExtendedLicenseStatus.ExpiringSoon:
                    return "warning";
                case ExtendedLicenseStatus.Expired:
                case ExtendedLicenseStatus.Invalid:
                case ExtendedLicenseStatus.Cracked:
                case ExtendedLicenseStatus.DeviceMismatch:
                case ExtendedLicenseStatus.FileNotFound:
                case ExtendedLicenseStatus.NotInitialized:
                    return "error";
                default:
                    return "unknown";
            }
        }
    }

    /// <summary>
    /// 许可证状态工具类
    /// </summary>
    public static class LicenseStatusHelper
    {
        /// <summary>
        /// 从QLicense状态和验证信息创建扩展状态信息
        /// </summary>
        /// <param name="originalStatus">原始状态</param>
        /// <param name="message">验证消息</param>
        /// <param name="license">许可证信息</param>
        /// <returns>扩展状态信息</returns>
        public static LicenseStatusInfo CreateExtendedStatus(LicenseStatus originalStatus, string message, MyTimeLicense license)
        {
            var statusInfo = new LicenseStatusInfo
            {
                OriginalStatus = originalStatus,
                Message = message,
                LicenseInfo = license,
                CheckTime = DateTime.Now
            };

            // 根据原始状态和详细信息确定扩展状态
            switch (originalStatus)
            {
                case LicenseStatus.VALID:
                    if (license != null)
                    {
                        if (license.IsExpired())
                        {
                            statusInfo.ExtendedStatus = ExtendedLicenseStatus.Expired;
                            statusInfo.Message = $"许可证已于 {license.ExpireDate:yyyy-MM-dd} 过期";
                        }
                        else if (license.IsExpiringSoon(30))
                        {
                            statusInfo.ExtendedStatus = ExtendedLicenseStatus.ExpiringSoon;
                        }
                        else
                        {
                            statusInfo.ExtendedStatus = ExtendedLicenseStatus.Valid;
                        }
                    }
                    else
                    {
                        statusInfo.ExtendedStatus = ExtendedLicenseStatus.Valid;
                    }
                    break;

                case LicenseStatus.INVALID:
                    // 根据消息内容确定具体的无效原因
                    if (message != null)
                    {
                        if (message.Contains("过期") || message.Contains("expired"))
                        {
                            statusInfo.ExtendedStatus = ExtendedLicenseStatus.Expired;
                        }
                        else if (message.Contains("设备") || message.Contains("device"))
                        {
                            statusInfo.ExtendedStatus = ExtendedLicenseStatus.DeviceMismatch;
                        }
                        else
                        {
                            statusInfo.ExtendedStatus = ExtendedLicenseStatus.Invalid;
                        }
                    }
                    else
                    {
                        statusInfo.ExtendedStatus = ExtendedLicenseStatus.Invalid;
                    }
                    break;

                case LicenseStatus.CRACKED:
                    statusInfo.ExtendedStatus = ExtendedLicenseStatus.Cracked;
                    break;

                case LicenseStatus.UNDEFINED:
                default:
                    statusInfo.ExtendedStatus = ExtendedLicenseStatus.Undefined;
                    break;
            }

            return statusInfo;
        }

        /// <summary>
        /// 创建文件不存在的状态信息
        /// </summary>
        /// <returns></returns>
        public static LicenseStatusInfo CreateFileNotFoundStatus()
        {
            return new LicenseStatusInfo
            {
                ExtendedStatus = ExtendedLicenseStatus.FileNotFound,
                OriginalStatus = LicenseStatus.INVALID,
                Message = "许可证文件不存在，需要激活软件",
                CheckTime = DateTime.Now
            };
        }

        /// <summary>
        /// 创建未初始化的状态信息
        /// </summary>
        /// <returns></returns>
        public static LicenseStatusInfo CreateNotInitializedStatus()
        {
            return new LicenseStatusInfo
            {
                ExtendedStatus = ExtendedLicenseStatus.NotInitialized,
                OriginalStatus = LicenseStatus.UNDEFINED,
                Message = "许可证管理器未初始化",
                CheckTime = DateTime.Now
            };
        }
    }
}